package org.hydrakyoufeng.vv.configuration;

import jakarta.servlet.*;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class VVConfig {

    /**
     * 编码过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean<Filter> encodingFilter() {
        Filter filter = new Filter() {
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8");
                chain.doFilter(request,response);
            }
        };
        // springboot提供的过滤器注册类
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.addUrlPatterns("*");
        registration.setFilter(filter);
        // 设置Filter先后级别，值越小越先执行
        registration.setOrder(2);
        return registration;
    }

}
